//
//  OrderSwitchCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/26.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class OrderSwitchCell: BaseTableViewCell {
    
    @IBOutlet weak var topLabel: UILabel!

    @IBOutlet weak var bottomLabel: UILabel!
    
    @IBOutlet weak var rightSwih: UISwitch!
    
    var totalModel :  OrderSureDataModel?

    var model : GBPInfoModel?{
    
    
        didSet{
        
            if model == nil {
                return
            }
        
            let number1 = Float(model!.available_bean) ?? 0
            let number2 = Float(model!.available_gold) ?? 0
            let number3 = Float(model!.available_predeposit) ?? 0
            
            if number1 > 0 {
                
                topLabel.text = "使用小鲸豆: (可用小鲸豆\(model!.available_bean)个)"
                
                let data = model!.available_bean.data(using: .unicode)
                
                if data != nil{
                    
                    let attrStr = try? NSAttributedString(data: model!.bean_desc.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSFontAttributeName : UIFont.systemFont(ofSize: 14)], documentAttributes: nil)
                    
                    bottomLabel.attributedText = attrStr
                    
                    
                }
                
                
                rightSwih.isOn = model!.isOn
                
   
            }
            
            if number2 > 0 {
                
                topLabel.text = "使用鲸币: (可用鲸币\(model!.available_gold)个)"
                
                
                let data = model!.available_gold.data(using: .unicode)
                
                if data != nil{
                    
                    let attrStr = try? NSAttributedString(data: model!.gold_desc.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSFontAttributeName : UIFont.systemFont(ofSize: 14)], documentAttributes: nil)
                    
                    bottomLabel.attributedText = attrStr
                    
                    
                }
                
                
                rightSwih.isOn = model!.isOn
 
                
            }
            
            if number3 > 0 {
             
                topLabel.text = "使用余额: (可用余额\(model!.available_predeposit)元)"
                
                let data = model!.available_predeposit.data(using: .unicode)
                
                if data != nil{
                    
                    let attrStr = try? NSAttributedString(data: model!.predeposit_desc.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSFontAttributeName : UIFont.systemFont(ofSize: 14)], documentAttributes: nil)
                    
                    bottomLabel.attributedText = attrStr
                    
                    
                }
                
                rightSwih.isOn = model!.isOn
                

            }

        
        
        
        }
    
    
    
    
    }

    
    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.white
    }

    @IBAction func rightSwihDidTap(_ sender: UISwitch) {
        
        QL1(sender.isOn)
        
        model?.isOn = sender.isOn
        
        
        var selet = false
        
        var  sumMoney : Float  = 0.0
        
        for item in totalModel!.GBPModels {
            
            if item.isOn == true {
                
                let number1 = Float(item.available_bean_amount) ?? 0
                let number2 = Float(item.available_gold_amount) ?? 0
                let number3 = Float(item.available_predeposit_amount) ?? 0
                
                if number1 > 0 {
                    
                    sumMoney += number1
                    
                }
                
                if number2 > 0 {
                    
                    sumMoney += number2

                }
                
                if number3 > 0 {
                    
                    sumMoney += number3

                }
                
               selet = true
                
            }
            
            
        }
        
        if selet == true {
            
            totalModel!.useGoldBean = true

        }else{
            
            totalModel!.useGoldBean = false

        
        }
        
        
        let number4 = Float(totalModel!.order_amount) ?? 0
        
        
         sumMoney = number4 - sumMoney
        
        if sumMoney <= 0.0 {
            
            totalModel!.needPayMoney = "0.00"
            
            for item in totalModel!.payment_list {
                item.isSelect = false
                item.allowTap = false
            }
            
        } else {
            
            for item in totalModel!.payment_list {
                item.allowTap = true
            }

            totalModel!.needPayMoney = "\(sumMoney)"

            
        }
        
        
          let tempVC =  viewController() as! BaseViewController


          tempVC.plainTableView.reloadData()
        
        
        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
